/**
 * 面、盒子工具
 */
class DrawPolygon {
  viewer:any;
  PolygonSource:any;
  polygonPoints:any;
  boxPoints:any;

  handlerBox:any;
  handlerPolygon:any;
  constructor(viewer:any, PolygonSource:any) {
    this.viewer = viewer;
    this.PolygonSource = PolygonSource;
  }
  // 绘制程序的准备工作，通过其他方式触发activate
  readyDrawPolygonHandler() {
    this.handlerPolygon = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Polygon)
    this.handlerPolygon.activeEvt.addEventListener((isActive) => {
      if (isActive) {
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
        document.documentElement.style.cursor = 'pointer'
      } else {
        this.viewer.enableCursorStyle = true
        document.documentElement.style.cursor = ''
      }
    })
    this.handlerPolygon.movingEvt.addEventListener(({ x, y }) => {
      // 绘制过程中的提示框，根据返回的屏幕坐标设置
      
    })
    this.handlerPolygon.drawEvt.addEventListener((result:any) => {
      // 绘制结束（不是关闭绘制）
      this.polygonPoints = result.object.positions;
    })
  }
  // 开始绘制
  startDrawPolygon() {
    this.handlerPolygon.deactivate() // 先触发一次关闭绘制
    this.handlerPolygon.clear() // 先清除一次之前的点位
    this.handlerPolygon.activate() // 触发绘制
  }
  // 清除已存在的点位 会同时关闭当前存在的绘制
  cleanDrawPolygon() {
    this.handlerPolygon.clear()
  }
  // 关闭绘制
  endDrawPolygon() {
    this.handlerPolygon.deactivate()
  }

  /**
   * 添加面
   * @author zhouziwei
   * @param {string} id 
   * @param {string} name 
   * @param {array} positions 经度和纬度值列表。值交替显示[经度，纬度，经度，纬度...]
   */
  drawLineHandler(id:string,name:string,positions:[]) {
    this.PolygonSource.entities.add({
      id,
      name,
      polygon: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights(positions),
        material: Cesium.Color.RED.withAlpha(0.5),
      }
    })
  }

   // 绘制盒子的准备工作，通过其他方式触发activate
  readyDraw3DPolygonHandler() {
    this.handlerBox = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Box);
    this.handlerBox.activeEvt.addEventListener((isActive) => {
      if (isActive) {
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
        document.documentElement.style.cursor = 'pointer'
      } else {
        this.viewer.enableCursorStyle = true
        document.documentElement.style.cursor = ''
      }
    })
    this.handlerBox.movingEvt.addEventListener(({ x, y }) => {
      // 绘制过程中的提示框，根据返回的屏幕坐标设置
      
    })
    this.handlerBox.drawEvt.addEventListener((result) => {
      // 绘制结束（不是关闭绘制）
      this.boxPoints = {
        position: result.object.position.getValue(),
        dimensions: result.object.box.dimensions.getValue()
      }
    })
  }

  // 开始绘制盒子
  startDraw3DPolygon() {
    this.handlerBox.deactivate() // 先触发一次关闭绘制
    this.handlerBox.clear() // 先清除一次之前的点位
    this.handlerBox.activate() // 触发绘制
  }

  // 清除已存在的点位 会同时关闭当前存在的绘制
  cleanDraw3DPolygon() {
    this.handlerBox.clear()
  }

  // 关闭绘制
  endDraw3DPolygon() {
    this.handlerBox.deactivate()
  }

  /**
   * @author zhouziwei
   * @param {string | number} id 
   * @param {string} name 
   * @param {array} position 经纬度
   * @param {array} dimensions 长宽高
   * @returns 
   */
  drawBoxHandler(id:string,name:string,position:[],dimensions:[]) {
    console.log(id,name,position,dimensions);
    
    let box = this.PolygonSource.entities.add({
      id,
      name,
      position: Cesium.Cartesian3.fromDegrees(...position),
      box: {
        dimensions: new Cesium.Cartesian3(...dimensions),
        material: Cesium.Color.RED.withAlpha(0.5),
        outline: true,
        outlineColor: Cesium.Color.BLACK,
      },
    })
    return box;
  }

  // 清除
  clean(id:string) {
    // viewer.entities.removeAll() // 清除场景中所有的entity实体
    // viewer.entities.remove(box) // 传入指定的实体删除
    viewer.entities.remove(viewer.entities.getById(id)) // 根据id获取到对应的实体再触发删除
  }
}

export default DrawPolygon;